package com.example.idachuappone.utils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.DialogInterface.OnShowListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.idachuappone.R;
import com.lidroid.xutils.bitmap.BitmapGlobalConfig.BitmapCacheManagementTask;

public class ComUtil {

	static class AnonymousClass_1 implements OnShowListener {
		private final/* synthetic */Animation val$anim;
		private final/* synthetic */ImageView val$img_dg;

		AnonymousClass_1(ImageView imageView, Animation animation) {
			this.val$img_dg = imageView;
			this.val$anim = animation;
		}

		@Override
		public void onShow(DialogInterface dialog) {
			this.val$img_dg.startAnimation(this.val$anim);
		}
	}

	static class AnonymousClass_2 implements OnDismissListener {
		private final/* synthetic */ImageView val$img_dg;

		AnonymousClass_2(ImageView imageView) {
			this.val$img_dg = imageView;
		}

		@Override
		public void onDismiss(DialogInterface dialog) {
			this.val$img_dg.clearAnimation();
		}
	}

	public static void showToast(Context context, String msg) {
		Toast.makeText(context, msg, 0).show();
	}

	public static void showToastLong(Context context, String msg) {
		Toast.makeText(context, msg, 1).show();
	}

	public static Dialog createCusProgressSmallBlackBgDialog(Context context) {
		View view = LayoutInflater.from(context).inflate(R.layout.view_cus_progress_small_black_bg_dialog, null);
		ImageView img_dg = (ImageView) view.findViewById(R.id.img_dg);
		Animation anim = AnimationUtils.loadAnimation(context, R.anim.cus_dialog_progress_rotate);
		anim.setInterpolator(new LinearInterpolator());
		Dialog dialog = new Dialog(context, 2131361799);
		dialog.setContentView(view);
		dialog.setCanceledOnTouchOutside(false);
		dialog.setOnShowListener(new AnonymousClass_1(img_dg, anim));
		dialog.setOnDismissListener(new AnonymousClass_2(img_dg));
		return dialog;
	}

	public static Dialog createCusProgressBigBlackBgDialog(Context context) {
		View view = LayoutInflater.from(context).inflate(R.layout.view_cus_progress_big_black_bg_dialog, null);
		int width = ((Activity) context).getWindowManager().getDefaultDisplay().getWidth();
		Dialog dialog = new Dialog(context, 2131361799);
		dialog.setContentView(view);
		Window window = dialog.getWindow();
		LayoutParams wl = window.getAttributes();
		wl.width = (int) ((width) * 0.6d);
		wl.height = width / 2;
		window.setAttributes(wl);
		dialog.setCanceledOnTouchOutside(false);
		return dialog;
	}

	public static int getAgeYear(String year) {
		return Calendar.getInstance().get(1) - Integer.parseInt(year);
	}

	public static int getAge(String birth) {
		try {
			Date birthDay = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA).parse(birth);
			Calendar cal = Calendar.getInstance();
			if (cal.before(birthDay)) {
				return -1;
			}
			int yearNow = cal.get(1);
			int monthNow = cal.get(BitmapCacheManagementTask.MESSAGE_FLUSH);
			int dayOfMonthNow = cal.get(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY);
			cal.setTime(birthDay);
			int yearBirth = cal.get(1);
			int monthBirth = cal.get(BitmapCacheManagementTask.MESSAGE_FLUSH);
			int dayOfMonthBirth = cal.get(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY);
			int age = yearNow - yearBirth;
			if (monthNow <= monthBirth) {
				if (monthNow != monthBirth) {
					age--;
				} else if (dayOfMonthNow < dayOfMonthBirth) {
					age--;
				}
			}
			return age;
		} catch (ParseException e) {
			return -1;
		}
	}

	public static String getTwoSum(String loc1, String loc2) {
		if (loc1 == null || loc1.trim().length() <= 0) {
			return "\u65e0\u6cd5\u83b7\u53d6\u5730\u5740";
		}
		if (loc2 == null || loc2.trim().length() <= 0) {
			return "\u65e0\u6cd5\u83b7\u53d6\u5730\u5740";
		}
		String[] a = loc1.split(",");
		String[] b = loc2.split(",");
		double c = getDistance(Double.valueOf(a[0]).doubleValue(), Double.valueOf(a[1]).doubleValue(),
				Double.valueOf(b[0]).doubleValue(), Double.valueOf(b[1]).doubleValue());
		String sum = "";
		if (c >= 1000.0d) {
			return new StringBuilder(String.valueOf(FormatDistance(new StringBuilder(String.valueOf(c / 1000.0d))
					.toString()))).append("KM").toString();
		}
		if (("".lastIndexOf(".")) + c > 0.0d) {
			sum = new StringBuilder(String.valueOf(c)).toString().substring(0,
					new StringBuilder(String.valueOf(c)).toString().lastIndexOf("."));
		}
		return new StringBuilder(String.valueOf(sum)).append("M").toString();
	}

	public static String FormatDistance(String distance) {
		return new DecimalFormat("0.00").format(new BigDecimal(distance));
	}

	public static double getDistance(double longitude1, double latitude1, double longitude2, double latitude2) {
		double Lat1 = rad(latitude1);
		double Lat2 = rad(latitude2);
		return Math.round(10000.0d * ((2.0d * Math.asin(Math.sqrt(Math.pow(Math.sin((Lat1 - Lat2) / 2.0d), 2.0d)
				+ ((Math.cos(Lat1) * Math.cos(Lat2)) * Math.pow(Math.sin((rad(longitude1) - rad(longitude2)) / 2.0d),
						2.0d))))) * 6378137.0d)) / 10000;
	}

	private static double rad(double d) {
		return (3.141592653589793d * d) / 180.0d;
	}

	public static String FormatCalenderTime3(String strDate) {
		try {
			return new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA).format(new SimpleDateFormat(
					"yyyy\u5e74MM\u6708dd\u65e5 HH:mm", Locale.CHINA).parse(strDate));
		} catch (Exception e) {
			return strDate;
		}
	}

	public static String FormatCalenderTime2(String strDate) {
		try {
			String front = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA).format(new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss", Locale.CHINA).parse(strDate));
			return front.startsWith(new StringBuilder(String.valueOf(Calendar.getInstance().get(1))).toString()) ? front
					.substring(front.indexOf("-") + 1)
					: front;
		} catch (Exception e) {
			return strDate;
		}
	}

	public static String FormatCalenderTime1(String strDate) {
		try {
			Date oriDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA).parse(strDate);
			Calendar cal = Calendar.getInstance();
			cal.setTime(oriDate);
			int week = cal.get(BitmapCacheManagementTask.MESSAGE_CLEAR_BY_KEY);
			String weekValue = "";
			switch (week) {
			case 1 /* 1 */:
				weekValue = "\u5468\u65e5";
				break;
			case BitmapCacheManagementTask.MESSAGE_FLUSH /* 2 */:
				weekValue = "\u5468\u4e00";
				break;
			case BitmapCacheManagementTask.MESSAGE_CLOSE /* 3 */:
				weekValue = "\u5468\u4e8c";
				break;
			case BitmapCacheManagementTask.MESSAGE_CLEAR /* 4 */:
				weekValue = "\u5468\u4e09";
				break;
			case BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY /* 5 */:
				weekValue = "\u5468\u56db";
				break;
			case BitmapCacheManagementTask.MESSAGE_CLEAR_DISK /* 6 */:
				weekValue = "\u5468\u4e94";
				break;
			case BitmapCacheManagementTask.MESSAGE_CLEAR_BY_KEY /* 7 */:
				weekValue = "\u5468\u516d";
				break;
			}
			String front = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA).format(oriDate);
			cal.setTime(new Date());
			return front.substring(0, front.indexOf(" ")) + "\uff08" + weekValue + "\uff09"
					+ front.substring(front.indexOf(" ") + 1);
		} catch (Exception e) {
			return strDate;
		}
	}

	public static long getDateLong(String date) {
		try {
			return new SimpleDateFormat("yyyy-MM-dd hh:mm").parse(date).getTime();
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}

	public static long getDateLong1(String date) {
		try {
			return new SimpleDateFormat("yyyy-MM-dd").parse(date).getTime();
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}
}
